<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidatePost{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateTitle = new Zend_Validate();
        $validateAlias = new Zend_Validate();
        $validateImage = new Zend_Validate();
        $validateDescription = new Zend_Validate();
        $validateContent = new Zend_Validate();
        $validateCategory = new Zend_Validate();
        
        $option = array('table' => 'posts', 'field' => 'title');
        
        if($edit){
            $option['exclude'] = array ('field' => 'id', 'value' => $params['id']);
        }
        
        $validateTitle->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 100)), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($option), true);
        
        $validateCategory->addValidator(new Zend_Validate_GreaterThan(0), true);
        
        $validateImage->addValidator(new Zend_Validate_NotEmpty(), true);
                        
        $validateDescription->addValidator(new Zend_Validate_NotEmpty(), true);
        
        $validateContent->addValidator(new Zend_Validate_NotEmpty(), true);
        
        /*
        * Check Title
        */
        
        if(!$validateTitle->isValid($params['title'])){
            $this->_messageError['title'] = '<label class="control-label" for="inputError">' . $translate->_('title') . current($validateTitle->getMessages()) . '</label>';
            $this->_arrData['title'] = '';
            $this->_classError['title'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['title'] = $data['title'];
            }
        }else{
            $this->_messageError['title'] = '';
            $this->_arrData['title'] = $params['title'];
            $this->_classError['title'] = '';
        }
        
        /*
        * Check Alias
        */
        
        $this->_messageError['alias'] = '';
        $this->_classError['alias'] = '';
        $this->_arrData['alias'] = $params['alias'];
        
        if(!$edit && strlen($params['alias']) > 0 || $params['alias'] != $data['alias']){
            $validateAlias->addValidator(new Zend_Validate_Alnum(true), true);
            
            if(!$validateAlias->isValid($params['alias'])){
                $this->_messageError['alias'] = '<label class="control-label" for="inputError">' . $translate->_('alias error') . '</label>';
                $this->_arrData['alias'] = '';
                $this->_classError['alias'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['alias'] = $data['alias'];
                }
            }
        }
        
        /*
        * Check Category
        */
        
        if(!$validateCategory->isValid($params['category'])){
            $this->_messageError['category'] = '<label class="control-label" for="inputError">' . $translate->_('category error') . '</label>';
            $this->_arrData['category'] = '';
            $this->_classError['category'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['category'] = $data['category'];
            }
        }else{
            $this->_messageError['category'] = '';
            $this->_arrData['category'] = $params['category'];
            $this->_classError['category'] = '';
        }
        
        /*
        * Check Image
        */
        
        if(!$validateImage->isValid($params['image'])){
            $this->_messageError['image'] = '<label class="control-label" for="inputError">' . $translate->_('image') . current($validateImage->getMessages()) . '</label>';
            $this->_arrData['image'] = '';
            $this->_classError['image'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['image'] = $data['image'];
            }
        }else{
            $this->_messageError['image'] = '';
            $this->_arrData['image'] = $params['image'];
            $this->_classError['image'] = '';
        }
        
        /*
        * Check Description
        */
        
        if(!$validateDescription->isValid($params['description'])){
            $this->_messageError['description'] = '<label class="control-label" for="inputError">' . $translate->_('description') . current($validateDescription->getMessages()) . '</label>';
            $this->_arrData['description'] = '';
            $this->_classError['description'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['description'] = $data['description'];
            }
        }else{
            $this->_messageError['description'] = '';
            $this->_arrData['description'] = $params['description'];
            $this->_classError['description'] = '';
        }
        
        /*
        * Check Content
        */
        
        if(!$validateContent->isValid($params['content'])){
            $this->_messageError['content'] = '<label class="control-label" for="inputError">' . $translate->_('content') . current($validateContent->getMessages()) . '</label>';
            $this->_arrData['content'] = '';
            $this->_classError['content'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['content'] = $data['content'];
            }
        }else{
            $this->_messageError['content'] = '';
            $this->_arrData['content'] = $params['content'];
            $this->_classError['content'] = '';
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $this->_arrData['status'] = $status;
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        if(!$this->isError() || strlen($params['alias']) == 0){
            $strConvert = new Zenddn_String_ConvertString();
            
            if(strlen($params['alias']) > 0){
                $str = $params['alias'];
            }else{
                $str = $params['title'];
            }
            
            $this->_arrData['alias'] = $strConvert->convert($str);
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        $info = new Zenddn_System_Info();
        $user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            $this->_arrData['created_by'] = $user_id;
        }
        
        $this->_arrData['modified'] = $curDate;
        $this->_arrData['modified_by'] = $user_id;
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}